package com.book.deprecated.state;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class DeprecatedCreateOrder extends DeprecateAbstractOrderState{

    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    private DeprecatedPayOrder deprecatedPayOrder;

    @Override
    protected DeprecatedOrder createOrder(String orderId, String productId, DeprecatedOrderContext context) {
        DeprecatedOrder order = DeprecatedOrder.builder()
                .orderId(orderId)
                .productId(productId)
                .state(ORDER_WAIT_PAY).build();

        // 新订单存入redis缓存15分钟后失效
        redisTemplate.opsForValue().set(orderId, JSON.toJSONString(order),15, TimeUnit.MINUTES);

        context.setCurrentState(this.deprecatedPayOrder);
        return order;
    }
}
